<template>
  <dialog-editor ref="dialog" :print-func='printHandle'></dialog-editor>
</template>

<script>
  import DialogEditor from '@/components/DialogEditor'
  import dialogMixins from '@/utils/commonDialogMixins'
  import {ProductLeavePrintModel} from '@/components/printModel/productLeavePrintModel'
  import {mRequest} from '@/utils/request'

  export default {
    mixins: [dialogMixins],
    components: {DialogEditor},
    data() {
      return {
        dataFormConfig: {
          id: {
            hidden: true
          },
          consignNoteId: {
            label: '发货通知单',
            type: 'list',
            list: [],
            placeholder: '请输入发货通知单'
          },
          clientId: {
            type: 'list',
            list: [],
            label: '客户名',
            filterable: true,
            clearable: true,
            showInMultiMode: true,
            placeholder: '请输入客户',
            selectCallback: this.clientIdSelectCallback
          },
          orderDetailId: {
            label: '订单',
            type: 'list',
            list: [],
            placeholder: '请输入订单',
            filterable: true,
            selectCallback: this.orderSelectCallback
          },
          productId: {
            label: '产品',
            type: 'list',
            list: [],
            //   disabled: true,
            filterable: true,
            clearable: true,
            placeholder: '请输入产品',
            selectCallback: this.productSelectCallback
          },
          productWeight: {
            label: '克数',
            type: 'number',
            max: 1000,
            default: 0
          },
          outTime: {
            label: '出库时间',
            type: 'date',
            showInMultiMode: true,
            placeholder: '请选择出库时间'
          },
          boxSupplyWay: {
            label: '纸箱供应方式',
            type: 'radio',
            radioBorder: true,
            radioList: [
              {text: '纸箱', label: 0}
            ],
            default: 0
          },
          // boxId: {
          //   label: '纸箱',
          //   type: 'list',
          //   placeholder: '请选择纸箱',
          //   list: [],
          //   reply: () => this.R.equals(0, this.dataForm['boxSupplyWay'])
          // },
          boxZhiNumber: {
            label: '箱只',
            type: 'number',
            reply: () => !this.R.equals(1, this.dataForm['boxSupplyWay']),
            selectCallback: this.numberChanged
          },
          boxNumber: {
            label: '箱数',
            type: 'number',
            max: 10000,
            default: 0,
            reply: () => !this.R.equals(1, this.dataForm['boxSupplyWay']),
            selectCallback: this.numberChanged
          },
          assortBoxNumber: {
            label: '配箱数量',
            type: 'number',
            max: 10000,
            default: 0,
            reply: () => this.R.equals(0, this.dataForm['boxSupplyWay']),
            selectCallback: this.numberChanged
          },
          assortBoxZhiNumber: {
            label: '配箱只数',
            type: 'number',
            max: 1000,
            default: 0,
            reply: () => this.R.equals(0, this.dataForm['boxSupplyWay']),
            selectCallback: this.numberChanged
          },
          tailBoxNumber: {
            label: '尾箱数量',
            type: 'number',
            max: 1000,
            default: 0,
            reply: () => this.R.equals(0, this.dataForm['boxSupplyWay']),
            selectCallback: this.numberChanged
          },
          tailBoxZhiNumber: {
            label: '尾箱只数',
            type: 'number',
            max: 1000,
            default: 0,
            reply: () => this.R.equals(0, this.dataForm['boxSupplyWay']),
            selectCallback: this.numberChanged
          },
          scrapNumber: {
            label: '零头',
            type: 'number',
            max: 100000,
            default: 0,
            selectCallback: this.numberChanged
          },

          realSaleNumber: {
            label: '出库数量',
            type: 'number',
            max: 1000000
          },
          weightCount: {
            label: '总克数',
            disabled: true
          },
          remark: {
            label: '备注',
            inputType: 'textarea',
            placeholder: '请输入备注'
          },
          status: {
            label: '状态',
            type: 'switch',
            activeValue: 1,
            inactiveValue: 0,
            default: 1
          }
        },
        editTableConfig: {
          orderDetailId: {
            title: '订单',
            type: 'select',
            optionsGet: () => {
              const filterData = this.orderListFilterByClientId.map(order => ({name: order.orderNo, id: order.id}))

              if (filterData.length > 0) {
                return filterData
              }
              return this.orderList.map(order => ({name: order.name, id: order.id}))
            },
            valueChangeEvent: (row) => {
              const {orderDetailId} = row

              this.orderSelectHandle(orderDetailId, data => {
                row.productId = this.R.pathOr(0, ['orderDetail', 'productId'], data)
                this.productSelectHandle(row.productId, subData => {
                  row.productWeight = this.R.pathOr(0, ['productInfo', 'productWeight'], subData)
                })
              })
            }
          },
          productId: {
            title: '产品名称',
            type: 'select',
            optionsGet: () => {
              return this.dataFormConfig.productId.list.map(item => ({name: item.name, id: item.id}))
            },
            valueChangeEvent: (row) => {
              const {productId} = row

              this.productSelectHandle(productId, data => {
                row.productWeight = this.R.pathOr(0, ['productInfo', 'productWeight'], data)
                this.weightChangedHandle(row)
              })
            }
          },
          productWeight: {
            title: '克数',
            default: 0,
            valueChangeEvent: this.weightChangedHandle
          },
          boxSupplyWay: {
            title: '包装方式',
            type: 'select',
            default: 0,
            optionsGet: () => {
              return [
                {name: '纸箱', id: 0}
              ]
            },
            valueChangeEvent: this.weightChangedHandle
          },
          boxZhiNumber: {
            title: '纸箱只数',
            type: 'number',
            default: 0,
            valueChangeEvent: this.weightChangedHandle
          },
          boxNumber: {
            title: '纸箱数量',
            type: 'number',
            default: 0,
            valueChangeEvent: this.weightChangedHandle
          },
          assortBoxNumber: {
            title: '配箱数量',
            type: 'number',
            default: 0,
            valueChangeEvent: this.weightChangedHandle
          },
          assortBoxZhiNumber: {
            title: '配箱只数',
            type: 'number',
            default: 0,
            valueChangeEvent: this.weightChangedHandle
          },
          tailBoxNumber: {
            title: '尾箱数量',
            type: 'number',
            default: 0,
            valueChangeEvent: this.weightChangedHandle
          },
          tailBoxZhiNumber: {
            title: '尾箱只数',
            type: 'number',
            default: 0,
            valueChangeEvent: this.weightChangedHandle
          },
          weightCount: {
            title: '总克数',
            type: 'number',
            default: 0
          },
          realSaleNumber: {
            title: '实际销售数量',
            type: 'number',
            default: 0
          },
          remark: {
            title: '备注',
            type: 'String'
          }
        },
        orderListFilterByClientId: [],
        orderList: [],
        multiAddProduct: false
      }
    },
    methods: {
      printHandle(params) {
        const {clientId, tableListData} = params
        const outTime = this.R.propOr('', 'outTime', params)
        const tickerNo = this.R.propOr('', 'tickerNo', params)
        const client = this.R.find(this.R.propEq('id', clientId), this.dataFormConfig.clientId.list)
        const clientName = client.name

        tableListData.forEach(row => {
          const {productId, orderDetailId} = row
          const product = this.R.find(this.R.propEq('id', productId), this.dataFormConfig.productId.list)
          const order = this.R.find(this.R.propEq('id', orderDetailId), this.dataFormConfig.orderDetailId.list)

          row.orderNo = order.name
          row.productNo = product.name
        })

        const productLeavePrintModel = new ProductLeavePrintModel(
          outTime, // 出库时间
          tickerNo, // 单号
          clientName, // 收货单位
          clientName, // 收货人
          '18827306618',// 收货人手机
          '单据', // 随车附物品
          '苏B00000', // 车号
          '苏大强', //驾驶员姓名
          '1887264438', // 驾驶员手机
          '厂家自费', // 运费
          tableListData // 出库数据数组
        )

        this.$vBus.$emit('SHOW_PRINT_DIALOG_V2', productLeavePrintModel, true)
      },
      productSelectHandle(productId, cb) {
        if (this.R.isNil(productId) || this.R.isEmpty(productId)) {
          return
        }
        mRequest('productApi/productInfoInfo', {id: productId}, ({data}) => {
          if (!(cb instanceof Function)) {
            return
          }
          return cb(data)
        })
      },
      orderSelectHandle(orderId, cb) {
        if (this.R.isNil(orderId) || this.R.isEmpty(orderId)) {
          return
        }
        mRequest('orderApi/orderDetailInfo', {id: orderId}, ({data}) => {
          if (!(cb instanceof Function)) {
            return
          }
          return cb(data)
        })
      },
      orderSelectCallback() {
        this.orderSelectHandle(this.dataForm.orderDetailId, (data) => {
          this.dataForm['productId'] = this.R.pathOr(0, ['orderDetail', 'productId'], data)
          this.dataForm['productWeight'] = this.R.pathOr(0, ['orderDetail', 'productWeight'], data)
          this.dataForm['boxSupplyWay'] = this.R.pathOr(0, ['orderDetail', 'boxSupplyWay'], data)
        })
      },
      addNewClientInfo() {
        const cb = (newValue) => {
          this.initClientInfoList()
          this.dataForm.clientId = newValue.id
        }
        this.$parent.openClientInfoDialog(cb)

      },
      allOrderListGet() {
        mRequest('orderApi/getOrderDetailList', {clientId: this.dataForm.clientId}, ({data}) => {
          this.orderList = this.R.propOr([], 'list', data)
        })
      },
      clientIdSelectCallback() {
        if (this.multiAddProduct) {
          mRequest('orderApi/getOrderDetailListBtyClientId', {clientId: this.dataForm.clientId}, ({data}) => {
            this.orderListFilterByClientId = this.R.propOr([], 'list', data)
          })
        } else {
          mRequest('orderApi/getOrderDetailListByClientId', {clientId: this.dataForm.clientId}, ({data}) => {
            this.dataFormConfig['orderDetailId'].list = this.R.propOr([], 'list', data)
          })
        }
      },
      productSelectCallback(value) {
        mRequest('productApi/productInfoInfo', {id: value}, (res) => {
          const productInfo = this.R.path(['data', 'productInfo'], res)
          this.dataForm['productWeight'] = productInfo['productWeight']
        })
      },
      numberChanged() {
        const packType = this.R.propOr(0, 'boxSupplyWay', this.dataForm)
        const productWeight = this.R.propOr(0, 'productWeight', this.dataForm)

        if (packType === 0) {
          const boxNumber = this.R.propOr(0, 'boxNumber', this.dataForm)
          const boxZhiNumber = this.R.propOr(0, 'boxZhiNumber', this.dataForm)
          const scrapNumber = this.R.propOr(0, 'scrapNumber', this.dataForm)
          const assortBoxNumber = this.R.propOr(0, 'assortBoxNumber', this.dataForm)
          const assortBoxZhiNumber = this.R.propOr(0, 'assortBoxZhiNumber', this.dataForm)
          const tailBoxNumber = this.R.propOr(0, 'tailBoxNumber', this.dataForm)
          const tailBoxZhiNumber = this.R.propOr(0, 'tailBoxZhiNumber', this.dataForm)

          this.dataForm.realSaleNumber = boxNumber * boxZhiNumber + assortBoxNumber * assortBoxZhiNumber + tailBoxNumber * tailBoxZhiNumber + scrapNumber
        }
        this.dataForm.weightCount = this.dataForm.realSaleNumber * productWeight
      },
      weightChangedHandle(row) {
        const {boxSupplyWay} = row
        const productWeight = this.R.propOr(0, 'productWeight', row)

        if (boxSupplyWay !== 1) {
          const boxZhiNumber = this.R.propOr(0, 'boxZhiNumber', row)
          const boxNumber = this.R.propOr(0, 'boxNumber', row)

          const assortBoxNumber = this.R.propOr(0, 'assortBoxNumber', row)
          const assortBoxZhiNumber = this.R.propOr(0, 'assortBoxZhiNumber', row)
          const tailBoxNumber = this.R.propOr(0, 'tailBoxNumber', row)
          const tailBoxZhiNumber = this.R.propOr(0, 'tailBoxZhiNumber', row)


          row['realSaleNumber'] = boxNumber * boxZhiNumber + assortBoxNumber * assortBoxZhiNumber + tailBoxNumber * tailBoxZhiNumber
          row['weightCount'] = row['realSaleNumber'] * productWeight
        }
        if (boxSupplyWay === 1) {
          const trayEachZhiNumber = this.R.propOr(0, 'trayEachZhiNumber', row)
          const trayLayerNumber = this.R.propOr(0, 'trayLayerNumber', row)
          const trayNumber = this.R.propOr(0, 'trayNumber', row)

          row['realSaleNumber'] = trayEachZhiNumber * trayLayerNumber * trayNumber
          row['weightCount'] = row['realSaleNumber'] * productWeight
        }
      },
      init(id, multiAdd) {
        this.dataForm.id = id || 0
        this.initProductInfoList()
        this.initOrderDetailList()
        this.initClientInfoList('clientId')
        // this.initBoxInfoIdList('boxId')
        // this.initTrayList()
        this.allOrderListGet()
        this.multiAddProduct = multiAdd
        const props = {
          dataForm: this.dataForm,
          config: this.dataFormConfig,
          dataRule: this.getNeedDataRule('clientId', 'productId', 'productOutNumber', 'outTime', 'tickerNo', 'trayNumber', 'boxNumber', 'status'),
          initApi: 'productApi/productLeaveStorageInfo',
          saveApi: 'productApi/saveProductLeaveStorage',
          updateApi: 'productApi/updateProductLeaveStorage',
          objectName: 'productLeaveStorage'
        }

        if (multiAdd) {
          props['multiSaveApi'] = 'productApi/multipleSaveProductLeaveStorage'
          props['editTableConfig'] = this.editTableConfig
        }
        this.$refs.dialog.initDialog(props)

        this.$nextTick(() => {
          const dataForm = this.dataForm

          Object.defineProperty(dataForm, 'weightCount', {
            get() {
              return dataForm['realSaleNumber'] * dataForm['productWeight']
            }
          })
        })
      }
    }
  }
</script>
